%% HY 200 - EPISTHMONIKOS YPOLOGISMOS
%% Ergasia 1
%% onomatepwnymo = Passas Virgilios
%% AEM = 784

x = [0 1 2 3 4 5];  %HMERES

y = [7670 7607.4 7544.6 7561.2 7526.4 7515.3];   %TSE 300
z = [1860 1848.2 1836.5 1860.2 1843.4 1805.2];   %NASDAQ
h = x(2)-x(1);
%% i)Kwdikas gia mia kampylh fysikhs kyvikhs paremvolis
A = [4 1 0 0;1 4 1 0;0 1 4 1;0 0 1 4];
bt = [y(1)-2*y(2)+y(3);y(2)-2*y(3)+y(4);y(3)-2*y(4)+y(5);y(4)-2*y(5)+y(6)];   
bt = (6/(h^2))*bt;                          %b tou TSE 300
bn = [z(1)-2*z(2)+z(3);z(2)-2*z(3)+z(4);z(3)-2*z(4)+z(5);z(4)-2*z(5)+z(6)];
bn = (6/(h^2))*bn;                          %b tou NASDAQ
m0 = 0;
m6 = 0;
temp1 = A\bt;                                  %m tou TSE 300
mt = [m0 temp1(1) temp1(2) temp1(3) temp1(4) m6];
temp2 = A\bn;                                  %m tou NASDAQ
mn = [m0 temp2(1) temp2(2) temp2(3) temp2(4) m6];

ajt = y;                                     %aj tou TSE 300
ajn = z;                                     %aj tou NASDAQ

for i=1:5
    bjt(i) = (y(i+1)-y(i))/h - ((mt(i+1)+2*mt(i))/6)*h;  %bj tou TSE 300
end

for i=1:5
    bjn(i) = (z(i+1)-z(i))/h - ((mn(i+1)+2*mn(i))/6)*h;  %bj tou NASDAQ
end

for i=1:5
    cjt(i) = mt(i)/2;                        %cj tou TSE 300
end

for i=1:5
    cjn(i) = mn(i)/2;                        %cj tou NASDAQ
end

for i=1:5
    djt(i) = (mt(i+1)-mt(i))/(6*h);             %dj tou TSE 300
end

for i=1:5
    djn(i) = (mn(i+1)-mn(i))/(6*h);              %dj tou NASDAQ
end

figure(1);
xInt1=0:0.01:5;
yInt1=interp1(x,y,xInt1,'spline');
ka1=spline(x,y,2.5);
plot(x,y,'b-',2.5,ka1,'r*')
hold on
plot(x,y,'o',xInt1,yInt1,'g')
axis([0,5,7500,7680]);
hold off

figure(2);
xInt2=0:0.01:5;
yInt2=interp1(x,z,xInt2,'spline');
ka2=spline(x,z,2.5);
plot(x,z,'b-',2.5,ka2,'r*')
hold on
plot(x,z,'o',xInt2,yInt2,'g')
axis([0,5,1800,1870]);
hold off

yy1=ajt(2) + bjt(2).*(2.5-x(2)) + cjt(2).*(2.5 - x(2)).^2 + djt(2).*(2.5 - x(2)).^3
yy2=ajn(2) + bjn(2).*(2.5-x(2)) + cjn(2).*(2.5 - x(2)).^2 + djn(2).*(2.5 - x(2)).^3


%% ii)Kwdikas gia mia kyvikh spline paremvolhs me paravolikh symperifora
%% sta akra

A = [5 1 0 0;1 4 1 0;0 1 4 1;0 0 1 5];
bt = [y(1)-2*y(2)+y(3);y(2)-2*y(3)+y(4);y(3)-2*y(4)+y(5);y(4)-2*y(5)+y(6)];   
bt = (6/(h^2))*bt;                          %b tou TSE 300
bn = [z(1)-2*z(2)+z(3);z(2)-2*z(3)+z(4);z(3)-2*z(4)+z(5);z(4)-2*z(5)+z(6)];
bn = (6/(h^2))*bn;                          %b tou NASDAQ

temp1 = A\bt;                                  %m tou TSE 300
mt = [temp1(1) temp1(1) temp1(2) temp1(3) temp1(4) temp1(4)];
temp2 = A\bn;                                   %m tou NASDAQ
mn = [temp2(1) temp2(1) temp2(2) temp2(3) temp2(4) temp2(4)];

ajt = y;                                     %aj tou TSE 300
ajn = z;                                     %aj tou NASDAQ

for i=1:5
    bjt(i) = (y(i+1)-y(i))/h - ((mt(i+1)+2*mt(i))/6)*h;  %bj tou TSE 300
end

for i=1:5
    bjn(i) = (z(i+1)-z(i))/h - ((mn(i+1)+2*mn(i))/6)*h;  %bj tou NASDAQ
end

for i=1:5
    cjt(i) = mt(i)/2;                        %cj tou TSE 300
end

for i=1:5
    cjn(i) = mn(i)/2;                        %cj tou NASDAQ
end

for i=1:5
    djt(i) = (mt(i+1)-mt(i))/(6*h);             %dj tou TSE 300
end

for i=1:5
    djn(i) = (mn(i+1)-mn(i))/(6*h);              %dj tou NASDAQ
end

figure(1);
xInt1=0:0.01:5;
yInt1=interp1(x,y,xInt1,'spline');
ka1=spline(x,y,2.5);
plot(x,y,'b-',2.5,ka1,'r*')
hold on
plot(x,y,'o',xInt1,yInt1,'g')
axis([0,5,7500,7680]);
hold off

figure(2);
xInt2=0:0.01:5;
yInt2=interp1(x,z,xInt2,'spline');
ka2=spline(x,z,2.5);
plot(x,z,'b-',2.5,ka2,'r*')
hold on
plot(x,z,'o',xInt2,yInt2,'g')
axis([0,5,1800,1870]);
hold off

yy1=ajt(2) + bjt(2).*(2.5-x(2)) + cjt(2).*(2.5 - x(2)).^2 + djt(2).*(2.5 - x(2)).^3
yy2=ajn(2) + bjn(2).*(2.5-x(2)) + cjn(2).*(2.5 - x(2)).^2 + djn(2).*(2.5 - x(2)).^3

 %% iii)Kwdikas gia mia kyvikh spline paremvolhs me eniaia kyvikh
 %% symperifora sta akra
 
A = [6 0 0 0;1 4 1 0;0 1 4 1;0 0 0 6];
bt = [y(1)-2*y(2)+y(3);y(2)-2*y(3)+y(4);y(3)-2*y(4)+y(5);y(4)-2*y(5)+y(6)];   
bt = (6/(h^2))*bt;                          %b tou TSE 300
bn = [z(1)-2*z(2)+z(3);z(2)-2*z(3)+z(4);z(3)-2*z(4)+z(5);z(4)-2*z(5)+z(6)];
bn = (6/(h^2))*bn;                          %b tou NASDAQ

temp1 = A\bt;                                  %m tou TSE 300
m0 = 2*temp1(1)-temp1(2);
m6 = 2*temp1(4)-temp1(3);
mt = [m0 temp1(1) temp1(2) temp1(3) temp1(4) m6];
temp2 = A\bn;                                  %m tou NASDAQ
mo = 2*temp2(1)-temp2(2);
mvi = 2*temp1(4)-temp1(3);
mn = [mo temp2(1) temp2(2) temp2(3) temp2(4) mvi];

ajt = y;                                     %aj tou TSE 300
ajn = z;                                     %aj tou NASDAQ

for i=1:5
    bjt(i) = (y(i+1)-y(i))/h - ((mt(i+1)+2*mt(i))/6)*h;  %bj tou TSE 300
end

for i=1:5
    bjn(i) = (z(i+1)-z(i))/h - ((mn(i+1)+2*mn(i))/6)*h;  %bj tou NASDAQ
end

for i=1:5
    cjt(i) = mt(i)/2;                        %cj tou TSE 300
end

for i=1:5
    cjn(i) = mn(i)/2;                        %cj tou NASDAQ
end

for i=1:5
    djt(i) = (mt(i+1)-mt(i))/(6*h);             %dj tou TSE 300
end

for i=1:5
    djn(i) = (mn(i+1)-mn(i))/(6*h);              %dj tou NASDAQ
end

figure(1);
xInt1=0:0.01:5;
yInt1=interp1(x,y,xInt1,'spline');
ka1=spline(x,y,2.5);
plot(x,y,'b-',2.5,ka1,'r*')
hold on
plot(x,y,'o',xInt1,yInt1,'g')
axis([0,5,7500,7680]);
hold off

figure(2);
xInt2=0:0.01:5;
yInt2=interp1(x,z,xInt2,'spline');
ka2=spline(x,z,2.5);
plot(x,z,'b-',2.5,ka2,'r*')
hold on
plot(x,z,'o',xInt2,yInt2,'g')
axis([0,5,1800,1870]);
hold off

yy1=ajt(2) + bjt(2).*(2.5-x(2)) + cjt(2).*(2.5 - x(2)).^2 + djt(2).*(2.5 - x(2)).^3
yy2=ajn(2) + bjn(2).*(2.5-x(2)) + cjn(2).*(2.5 - x(2)).^2 + djn(2).*(2.5 - x(2)).^3
